package com.didibigdata.Repositories;

import com.didibigdata.entities.OrderTime;
import com.didibigdata.entities.OrderTimeType;
import com.didibigdata.utils.JDBCTools;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lkabb on 2020/6/7
 */
public class OrderTimeRepository {

    public List<OrderTimeType> getOrderTimeTypeList() {
        //日志
        Logger logger = LoggerFactory.getLogger(OrderTimeRepository.class);
        logger.info("add OrderTimeRepository logger successful!");

        ResultSet resOfOrderTime = null;
        Connection connectionmysql = null;
        PreparedStatement statementmysql = null;
        List<OrderTime> orderTimeList = new ArrayList<OrderTime>();
        List<OrderTimeType> orderTimeTypeList = new ArrayList<OrderTimeType>();
        try {
            connectionmysql = JDBCTools.getConnection();
            String sqlOrderTime = "select day,count,flag from t_didi2016_fenduan";
            statementmysql = connectionmysql.prepareStatement(sqlOrderTime);
            logger.info("connect to JDBC and executeQuery [{}]", sqlOrderTime);
            resOfOrderTime = statementmysql.executeQuery();

            //初始化订单时长范围数组
            int[] countOfSection = new int[6];
            for (int i = 0; i < 6 ; i++ ) {
                countOfSection[i] = 0;
            }

            while (resOfOrderTime.next()) {
                OrderTime orderTime = new OrderTime();
                //生成订单时长List
                orderTime.setDay(resOfOrderTime.getString(1));
                orderTime.setCount(Integer.parseInt(resOfOrderTime.getString(2)));
                orderTime.setFlag(Integer.parseInt(resOfOrderTime.getString(3)));
                orderTimeList.add(orderTime);

                switch (orderTime.getFlag()) {
                    case 1:
                        countOfSection[0] += orderTime.getCount();
                        break;
                    case 2:
                        countOfSection[1] += orderTime.getCount();
                        break;
                    case 3:
                        countOfSection[2] += orderTime.getCount();
                        break;
                    case 4:
                        countOfSection[3] += orderTime.getCount();
                        break;
                    case 5:
                        countOfSection[4] += orderTime.getCount();
                        break;
                    case 6:
                        countOfSection[5] += orderTime.getCount();
                        break;
                }

            }

            for (int i = 0; i < 6; i++) {
                OrderTimeType orderTimeType = new OrderTimeType();
                orderTimeType.setTypeCount(countOfSection[i]);
                switch (i + 1) {
                    case 1:
                        orderTimeType.setType("0-5分钟");
                        break;
                    case 2:
                        orderTimeType.setType("5-15分钟");
                        break;
                    case 3:
                        orderTimeType.setType("15-30分钟");
                        break;
                    case 4:
                        orderTimeType.setType("30-45分钟");
                        break;
                    case 5:
                        orderTimeType.setType("45-60分钟");
                        break;
                    case 6:
                        orderTimeType.setType("60分钟以上");
                        break;
                }
                orderTimeTypeList.add(orderTimeType);
            }
            logger.info("set orderTimeType successful!");

        } catch (Exception e) {
            logger.error("OrderTimeRepository SQLException: [{}]", e);
            e.printStackTrace();
        } finally {
            JDBCTools.releaseConnection(connectionmysql, statementmysql, resOfOrderTime);
        }

        return orderTimeTypeList;
    }
}
